package edu.lingnan.librarymanage.controller;

import com.google.code.kaptcha.Producer;
import edu.lingnan.librarymanage.core.base.AjaxResult;
import edu.lingnan.librarymanage.core.constant.LibraryManageConstant;
import edu.lingnan.librarymanage.core.vo.BookVO;
import edu.lingnan.librarymanage.core.vo.CategoryVO;
import edu.lingnan.librarymanage.service.BookService;
import edu.lingnan.librarymanage.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cql
 */
@Controller
@RequestMapping("/common")
public class CommonController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BookService bookService;

    @Resource(name = "captchaProducer")
    private Producer captchaProducer;


    @ResponseBody
    @GetMapping("/category")
    public AjaxResult getAllCategory() {
        List<CategoryVO> result = categoryService.findAllCategory();
        return AjaxResult.success(result);
    }

    @ResponseBody
    @GetMapping("/type")
    public AjaxResult getCategoryLevel1() {
        List<CategoryVO> result = categoryService.findAllCategory().stream().filter(
                f -> {
                    return f.getLevel() == 1;
                }
        ).collect(Collectors.toList());
        return AjaxResult.success(result);
    }

    /**
     * 生成验证码
     */
    @GetMapping("/captchaImage")
    public ModelAndView getCode(HttpServletResponse response, HttpSession session) throws IOException {

        //生成验证码
        String code = captchaProducer.createText();
        BufferedImage image = captchaProducer.createImage(code);

        /**
         * 将验证码和验证码过期时间存储进session
         */
        session.setAttribute(LibraryManageConstant.CODE, code);
        session.setAttribute(LibraryManageConstant.EXPIRES, System.currentTimeMillis());

        // 转换流信息写出
        ServletOutputStream out = response.getOutputStream();
        // write the data out
        ImageIO.write(image, "jpg", out);
        try {
            out.flush();
        } finally {
            out.close();//关闭
        }
        return null;
    }


    @ResponseBody
    @GetMapping("/book/{bookId}")
    public AjaxResult findBookDetail(@PathVariable("bookId") Integer bookId) {
        BookVO bookVO = bookService.findBookDetail(bookId);
        return AjaxResult.success(bookVO);
    }

}
